<template>
  <div class="channel-edit">
    <van-cell>
      <div slot="title" class="title-text">我的频道</div>
      <van-button
      class="edit-btn"
      type="danger"
      size="mini"
      plain
      round
      @click="isEdit = !isEdit">
      {{isEdit ? '完成': '编辑'}}</van-button>
    </van-cell>
    <van-grid :gutter="10" class="my-grid">
      <van-grid-item
      class="grid-item"
      v-for="(channel, index) in myChannels"
      :key="index"
      @click="onMyChannelClick(channel, index)">
      <van-icon
      slot="icon"
      name="clear"
      v-show="isEdit && !fiexdChannels.includes(channel.id)">
      </van-icon>
      <span class="text" :class="{active: index === active}" slot="text">
        {{channel.name}}
      </span>
      </van-grid-item>
    </van-grid>
    <van-cell :border="false">
      <div slot="title" class="title-text">频道推荐</div>
    </van-cell>
    <van-grid :gutter="10" class="recommend-grid">
      <van-grid-item
      class="grid-item"
      v-for="(channel, index) in recommendChannels"
      :key="index"
      :text="channel.name"
      @click="onAddChannel(channel)"/>
    </van-grid>
  </div>
</template>

<script>
import { getAllChannels, addUserChannel, deleteUserChannel } from '@/api/channel.js'
import { mapState } from 'vuex'
import { setItem } from '@/utils/storage.js'
export default {
  name: 'ChannelEdit',
  components: {
  },
  props: {
    myChannels: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      allChannels: [],
      isEdit: false, // 控制编辑状态的显示
      fiexdChannels: [0] // 固定频道的id，不允许删除
    }
  },
  computed: {
    recommendChannels () {
      const channels = []
      this.allChannels.forEach(channel => {
        const ret = this.myChannels.find(myChannel => {
          return myChannel.id === channel.id
        })
        if (!ret) {
          channels.push(channel)
        }
      })
      return channels
    },
    ...mapState(['user'])
  },
  watch: {},
  created () {
    this.loadAllChannels()
  },
  mounted () {},
  methods: {
    async loadAllChannels () {
      try {
        const { data } = await getAllChannels()
        this.allChannels = data.data.channels
      } catch (err) {
        console.log(err)
      }
    },

    async onAddChannel (channel) {
      this.myChannels.push(channel)
      // 进行判断看是否登录
      if (this.user) {
        // 已登录则将数据存到用户信息中
        try {
          await addUserChannel({
            id: channel.id, // 频道 id
            seq: this.myChannels.length // 频道序号
          })
        } catch (err) {
          this.$toast('保存失败稍后重试')
        }
      } else {
        setItem('TOUTIAO_CHANNELS', this.myChannels)
      }
    },

    onMyChannelClick (channel, index) {
      // console.log(channel, index)
      // 判断编辑按钮的状态
      if (this.isEdit) {
        // 编辑状态删除频道
        // 进行判断看是否为固定项
        if (this.fiexdChannels.includes(channel.id)) {
          return
        }
        // 删除频道
        this.myChannels.splice(index, 1)
        // 进行判断如果删除的是当前频道的前面项则将active-1
        if (index <= this.active) {
          this.$emit('update-active', this.active - 1, true)
        }
        // 处理数据持久化
        this.deleteChannel(channel)
      } else {
        // 非编辑状态切换频道
        this.$emit('update-active', index, false)
      }
    },

    async deleteChannel (channel) {
      // 判断用户登录状态
      try {
        if (this.user) {
        // 已登录则将数据从用户信息中删除
          await deleteUserChannel(channel.id)
        } else {
        // 未登录则将将本地存储的数据进行更新
          setItem('TOUTIAO_CHANNELS', this.myChannels)
        }
      } catch (err) {
        this.$toast('删除数据失败，稍后重试')
      }
    }
  }
}
</script>

<style scoped lang="less">
.channel-edit {
  padding: 90px 0;
  .title-text {
    font-size: 32px;
    color: #333333;
  }
  .edit-btn{
  width: 104px;
  height: 48px;
  font-size: 26px;
  color: #f85959;
  border: 1px solid #f85959;
  }
  /deep/ .grid-item {
    width: 160px;
    height: 86px;
    .van-grid-item__content {
      // 不折行
      white-space: nowrap;
      background-color: #f4f5f6;
      .van-grid-item__text,
      .text {
        font-size: 28px;
        color: #222;
        margin-top: 0;
      }
      .active {
        color: red;
      }
      .van-grid-item__icon-wrapper {
        position: unset;
      }
    }
  }
  /deep/ .my-grid {
    .grid-item {
      .van-icon-clear {
        position: absolute;
        right: -10px;
        top: -10px;
        font-size: 35px;
        color: black;
        z-index: 2;
      }
    }
  }
  /deep/ .recommend-grid {
    .grid-item {
      .van-grid-item__content {
       flex-direction: row;
      // .van-icon-plus {
      //   font-size: 28px;
      //   margin-right: 5px;
      // }
      // .van-grid-item__text
      //   margin-top: 0;
      //
      }
    }
  }
}
</style>
